package com.example.administrator.wxam;

import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

public class Main2Activity extends AppCompatActivity {
    public ImageView topuxioan;
    private TextView zhiTI;
    private static RequestQueue requestQueue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        ZHaoId();
       requestQueue = Volley.newRequestQueue(this);
        MainActivity m = new MainActivity();
        StringRequest stringRequest = new StringRequest("http://api.gzkbapp.com/getUserInfo.action?userId=" + m.getUser_id() + "&ssnId=" + m.getSsn_id() + "", new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    JSONObject user_info = jsonObject.optJSONObject("user_info");
                    String ava_url = user_info.optString("ava_url");
                    String user_nknm = user_info.optString("user_nknm");
                    Main2Activity.setImageLoader(ava_url,topuxioan);
                    zhiTI.setText(user_nknm);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        requestQueue.add(stringRequest);
        requestQueue.start();
    }

    private void ZHaoId() {
        topuxioan = (ImageView) findViewById(R.id.ImV_rtupian);
        zhiTI = (TextView) findViewById(R.id.TV_wenzhi);
    }

    public static void setImageLoader(String url, ImageView imageView) {
        ImageLoader loader = new ImageLoader(requestQueue, new BitmapCache());
        ImageLoader.ImageListener imageListener = ImageLoader.getImageListener(imageView, R.mipmap.ic_launcher, R.mipmap.ic_launcher);
        loader.get(url, imageListener);
    }

}

class BitmapCache implements ImageLoader.ImageCache {
    private LruCache<String, Bitmap> mMemoryCache;
    public BitmapCache() {
        // Use 1/16th of the available memory for this memory cache.
        final int cacheSize = 1024 * 1024 *  16;
        mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
            @Override
            protected int sizeOf(String key, Bitmap value) {
                return value.getRowBytes() * value.getHeight();
            }
        };
    }
    @Override
    public Bitmap getBitmap(String url) {
        return mMemoryCache.get(url);
    }
    @Override
    public void putBitmap(String url, Bitmap bitmap) {
        mMemoryCache.put(url, bitmap);
    }


}
